﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using RM2012Server.Controller;
using CrystalDecisions.CrystalReports.Engine;
using RM2012Server.Business;
using CrystalDecisions.Shared;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frptInvoice : Office2007Form
    {
        #region Fields
        InvoiceCtrl m_InvoiceCtrl = new InvoiceCtrl();
        InvoiceDetailCtrl m_InvoiceDetailCtrl = new InvoiceDetailCtrl();
        CustomerCtrl m_CustomerCtrl = new CustomerCtrl();
        TablesCtrl m_TablesCtrl = new TablesCtrl();
        #endregion

        #region Constructor
        public frptInvoice()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frptInvoice_Load(object sender, EventArgs e)
        {
            cmbFormat.SelectedIndex = 0;
            lblZoom.Text = sliderZoom.Value.ToString() + " %";
            txtInvoiceId.Text = GeneralInfo.invoiceIdReport;
            DataTable dtInvoiceReport = m_InvoiceCtrl.GetByIdForReport(txtInvoiceId.Text);
            txtCusName.Text = dtInvoiceReport.Rows[0]["FullName"].ToString();
            LoadReport();
        }
        #endregion

        #region Public Method
        public String GetPath()
        {
            String path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));
            path = path + "Report/crptInvoiceDetail.rpt";
            return path;
        }

        public ReportDocument GetDocument(String invoiceId)
        {
            ReportDocument doc = new ReportDocument();
            doc.Load(GetPath());
            doc.SetDataSource(GetSource(invoiceId));
            return doc;
        }

        public List<InvoiceReportInfo> GetSource(String invoiceId)
        {

            List<InvoiceReportInfo> list = new List<InvoiceReportInfo>();
            DataTable dt = m_InvoiceDetailCtrl.GetReportByInvoiceId(invoiceId);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    InvoiceReportInfo info = new InvoiceReportInfo();
                    info.MenuName = dt.Rows[i]["MenuName"].ToString();
                    info.Quantity = dt.Rows[i]["Quantity"].ToString();
                    if (info.Quantity.Contains("."))
                    {
                        info.Quantity = info.Quantity.Substring(0, info.Quantity.LastIndexOf("."));
                    }
                    info.Price = dt.Rows[i]["Price"].ToString();
                    if (info.Price.Contains("."))
                    {
                        info.Price = info.Price.Substring(0, info.Price.LastIndexOf("."));
                    }
                    info.UnitName = dt.Rows[i]["UnitName"].ToString();
                    list.Add(info);
                }
            }
            return list;
        }

        public void LoadReport()
        {
            try
            {
                DataTable dtInvoice = m_InvoiceCtrl.GetByIdForReport(txtInvoiceId.Text);
                String invoiceId = dtInvoice.Rows[0]["InvoiceId"].ToString();
                String cusName = dtInvoice.Rows[0]["FullName"].ToString();
                String tablesName = dtInvoice.Rows[0]["TablesName"].ToString();
                String total = dtInvoice.Rows[0]["Total"].ToString();
                String discountPer = dtInvoice.Rows[0]["DiscountPer"].ToString();
                String paid = dtInvoice.Rows[0]["Paid"].ToString();

                ParameterFields paramFields = new ParameterFields();

                //Parameter Full Name
                ParameterField field = new ParameterField();
                field.Name = "fullName";
                ParameterDiscreteValue pdv = new ParameterDiscreteValue();
                pdv.Value = cusName;
                field.CurrentValues.Add(pdv);

                //Parameter Report Code
                ParameterField field2 = new ParameterField();
                field2.Name = "codeReport";
                ParameterDiscreteValue pdv2 = new ParameterDiscreteValue();
                pdv2.Value = invoiceId;
                field2.CurrentValues.Add(pdv2);

                //Parameter Table Name
                ParameterField field3 = new ParameterField();
                field3.Name = "tableName";
                ParameterDiscreteValue pdv3 = new ParameterDiscreteValue();
                pdv3.Value = tablesName;
                field3.CurrentValues.Add(pdv3);

                //Parameter Date Report
                ParameterField field4 = new ParameterField();
                field4.Name = "dateReport";
                ParameterDiscreteValue pdv4 = new ParameterDiscreteValue();
                pdv4.Value = GetNow();
                field4.CurrentValues.Add(pdv4);

                //Parameter Total Money
                ParameterField field5 = new ParameterField();
                field5.Name = "totalMoney";
                ParameterDiscreteValue pdv5 = new ParameterDiscreteValue();
                pdv5.Value = total;
                field5.CurrentValues.Add(pdv5);

                //Parameter Discount Per
                ParameterField field6 = new ParameterField();
                field6.Name = "discountPer";
                ParameterDiscreteValue pdv6 = new ParameterDiscreteValue();
                pdv6.Value = discountPer;
                field6.CurrentValues.Add(pdv6);

                //Parameter Total Pay
                ParameterField field7 = new ParameterField();
                field7.Name = "totalPay";
                ParameterDiscreteValue pdv7 = new ParameterDiscreteValue();
                pdv7.Value = paid;
                field7.CurrentValues.Add(pdv7);

                //Add Parameter to List
                paramFields.Add(field);
                paramFields.Add(field2);
                paramFields.Add(field3);
                paramFields.Add(field4);
                paramFields.Add(field5);
                paramFields.Add(field6);
                paramFields.Add(field7);
                //Add Parameter List to CrystalReport Viewer
                viewer.ParameterFieldInfo = paramFields;
                viewer.ReportSource = GetDocument(invoiceId);
                //
                lblPageCurrent.Text = "1";
            }
            catch
            {

            }
        }

        public void SaveFormat(ExportFormatType type, String fileName)
        {
            try
            {
                ReportDocument docSave = new ReportDocument();
                docSave = (ReportDocument)viewer.ReportSource;
                docSave.ExportToDisk(type, fileName);
            }
            catch
            {

            }
        }

        public void ZoomDocument(int level)
        {
            viewer.Zoom(level);
        }

        public String GetNow()
        {
            DateTime dt = DateTime.Now;
            String str = dt.Day.ToString() + "/" + dt.Month.ToString() + "/" + dt.Year.ToString();
            return str;
        }
        #endregion

        #region Event Handles
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //PDF
                String fileName = dialog.FileName;
                if (cmbFormat.SelectedIndex == 0)
                {
                    fileName = fileName + ".pdf";
                    SaveFormat(ExportFormatType.PortableDocFormat, fileName);
                }
                else if (cmbFormat.SelectedIndex == 1)
                {
                    //WORD
                    fileName = fileName + ".doc";
                    SaveFormat(ExportFormatType.WordForWindows, fileName);
                }
                else if (cmbFormat.SelectedIndex == 2)
                {
                    //EXCEL
                    fileName = fileName + ".xls";
                    SaveFormat(ExportFormatType.Excel, fileName);
                }
                else if (cmbFormat.SelectedIndex == 3)
                {
                    //HTML 40
                    fileName = fileName + ".html";
                    SaveFormat(ExportFormatType.HTML40, fileName);
                }
                MessageBoxEx.Show("File đã được lưu tới " + fileName, "LƯU BÁO CÁO");
            }
        }

        private void sliderZoom_ValueChanged(object sender, EventArgs e)
        {
            ZoomDocument(sliderZoom.Value);
            lblZoom.Text = sliderZoom.Value.ToString() + " %";
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            viewer.PrintReport();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            viewer.ShowFirstPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            viewer.ShowPreviousPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
            if (viewer.GetCurrentPageNumber() == 1)
            {
                btnFirst.Enabled = false;
                btnPre.Enabled = false;
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPre.Enabled = true;
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            viewer.ShowNextPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            viewer.ShowLastPage();
            lblPageCurrent.Text = viewer.GetCurrentPageNumber().ToString();
            btnFirst.Enabled = true;
            btnPre.Enabled = true;
            btnNext.Enabled = false;
            btnLast.Enabled = false;
        }
        #endregion
    }
}
